package cn.com.nes.site.modules.excel.dto;

import cn.com.nes.site.controller.crm.opportunity.vo.ImportExcelOfEndCustomerVO;
import cn.com.nes.site.entity.em.crm.customer.CustomerNatureEnum;
import cn.com.nes.site.entity.em.crm.customer.CustomerSaveTypeEnum;
import cn.com.nes.site.entity.em.crm.customer.EnergyTypeEnum;
import cn.com.nes.site.entity.em.crm.opportunity.*;
import cn.com.nes.site.entity.em.crm.saleorder.SaleOrderPayModelEnum;
import cn.com.nes.site.entity.em.crm.saleorder.SaleOrderStatusEnum;
import cn.com.nes.site.entity.em.crm.saleorder.SaleOrderTypeEnum;
import cn.com.nes.site.entity.em.project.ProjectRelationDataTypeEnum;
import cn.com.nes.site.modules.excel.valid.ExcelCheck;
import cn.com.nes.site.modules.sale.bo.SaleBO;
import cn.com.nes.site.modules.sale.enums.SaleTypeEnum;
import cn.com.nes.site.service.crm.customer.bo.CustomerBO;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityBO;
import cn.com.nes.site.service.crm.order.bo.SaleDetailBO;
import cn.com.nes.site.service.crm.order.bo.SaleMasterBO;
import cn.com.nes.site.service.crm.order.bo.SaleOrderBO;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Data
public class EndCustomerDTO {

    @ExcelCheck(canEmpty = false, length = 64)
    @ExcelProperty(value = {"", "", "行政区划"}, index = 0)
    private String areaCode;

    @ExcelCheck(canEmpty = false, length = 256)
    @ExcelProperty(value = {"", "", "地址"}, index = 1)
    private String address;

    @ExcelProperty(value = {"", "", "门牌号"}, index = 2)
    private String houseNumber;

    @ExcelCheck(canEmpty = false, length = 64)
    @ExcelProperty(value = {"", "", "户主姓名"}, index = 3)
    private String contactName;

    @ExcelCheck(canEmpty = false, length = 18, canRepeat = false)
    @ExcelProperty(value = {"", "", "身份证号码"}, index = 4)
    private String idCardNumber;

    @ExcelCheck(canEmpty = false, length = 20)
    @ExcelProperty(value = {"", "", "电话号码"}, index = 5)
    private String contactPhone;

    @ExcelProperty(value = {"", "", "建筑面积"}, index = 6)
    private String housingArea;

    @ExcelProperty(value = {"", "", "建筑结构"}, index = 7)
    private String buildingStructure;

    @ExcelProperty(value = {"", "", "房屋类型"}, index = 8)
    private String houseType;

    @ExcelProperty(value = {"", "", "采暖末端"}, index = 9)
    private String heatingEnd;

    @ExcelProperty(value = {"", "", "计划年度"}, index = 10)
    private String planYear;

    @ExcelProperty(value = {"", "", "改造类型"}, index = 11)
    private String renovationType;

    @ExcelProperty(value = {"", "", "能源类型"}, index = 12)
    private String energyType;

    @ExcelProperty(value = {"", "", "电表号/燃气表号"}, index = 13)
    private String meterNumber;

    @ExcelProperty(value = {"", "", "供暖方式"}, index = 14)
    private String heatingMode;

    @ExcelCheck(length = 64)
    @ExcelProperty(value = {"", "", "设备型号"}, index = 15)
    private String deviceModel;

    @ExcelProperty(value = {"", "是否本村户籍"}, index = 16)
    private String isVillagers;

    @ExcelProperty(value = {"", "备注"}, index = 17)
    private String remark;

    // 扩展参数
    private BigDecimal longitude; // 经度

    private BigDecimal latitude; // 纬度


    public SaleBO buildSaleBO(ImportExcelOfEndCustomerVO vo) {
        SaleBO saleBO = new SaleBO();
        saleBO.setSaleType(SaleTypeEnum.GOVERNMENT_PERSONAL_SALES);
        saleBO.setHandleForExcelImport(Boolean.TRUE);

        // 构建 客户BO
        saleBO.setCustomerBO(buildCustomerBO(vo));

        // 构建 商务项目BO
        saleBO.setOpptunityBO(buildOpportunityBO(vo));

        // 构建 订单BO
        if (StrUtil.isNotEmpty(deviceModel)) {
            saleBO.setSaleOrderBO(buildOrderBO(vo));
        }
        return saleBO;
    }

    private OpportunityBO buildOpportunityBO(ImportExcelOfEndCustomerVO vo) {
        OpportunityBO opportunityBO = new OpportunityBO();
        BeanUtils.copyProperties(this, opportunityBO);
        BeanUtils.copyProperties(vo, opportunityBO);
        opportunityBO.setProjectRelationDataTypeEnum(ProjectRelationDataTypeEnum.EXCEL);
        opportunityBO.setOpportunityClassification(OpportunityClassificationEnum.END_USRE);
        opportunityBO.setRelateOpportunityUuid(vo.getMainOpportunityUuid());
        opportunityBO.setAddOpportunity(Boolean.TRUE);
        opportunityBO.setOpportunityName(contactName + " - 政府终端用户商机");
        opportunityBO.setPersonInCharge(contactName);
        opportunityBO.setPersonInChargePhone(contactPhone);
        opportunityBO.setOpportunityStage(OpportunityStageEnum._transaction);
        opportunityBO.setOpportunityType(OpportunityTypeEnum._purchase);
        opportunityBO.setProjectAddress(address);
        opportunityBO.setAreaCode(areaCode.substring(0, areaCode.indexOf("_")));
        if (StrUtil.isNotEmpty(isVillagers)) {
            opportunityBO.setIsVillage(isVillagers.substring(0, isVillagers.indexOf("_")));
        }
        if (StrUtil.isNotEmpty(housingArea)) {
            opportunityBO.setProjectArea(new BigDecimal(housingArea));
            opportunityBO.setNewlyDeclaredArea(new BigDecimal(housingArea));
        }
        if (StrUtil.isNotEmpty(houseType)) {
            opportunityBO.setHouseType(houseType.substring(0, houseType.indexOf("_")));
        }
        if (StrUtil.isNotEmpty(heatingEnd)) {
            opportunityBO.setHeatingEnd(heatingEnd.substring(0, heatingEnd.indexOf("_")));
        }
        if (StrUtil.isNotEmpty(buildingStructure)) {
            opportunityBO.setBuildingStructure(buildingStructure.substring(0, buildingStructure.indexOf("_")));
        }
        if (StrUtil.isNotEmpty(renovationType)) {
            opportunityBO.setRenovationType(RenovationType.getEnum(renovationType.substring(0, renovationType.indexOf("_"))));
        }
        if (StrUtil.isNotEmpty(remark)) {
            opportunityBO.setMemo(remark);
        }
        if (StrUtil.isNotEmpty(planYear)) {
            opportunityBO.setPlanYear(planYear);
        }
        if (StrUtil.isNotEmpty(energyType)) {
            opportunityBO.setEnergyType(EnergyTypeEnum.getEnum(energyType.substring(0, energyType.indexOf("_"))));
            opportunityBO.setMeterNumber(meterNumber);
        }
        if (StrUtil.isNotEmpty(heatingMode)) {
            opportunityBO.setHeatingMode(heatingMode.substring(0, heatingMode.indexOf("_")));
        }
        opportunityBO.setCategoryOfDeclaration(DeclareCategoryEnum.NEW);
        return opportunityBO;
    }

    private CustomerBO buildCustomerBO(ImportExcelOfEndCustomerVO vo) {
        CustomerBO customerBO = new CustomerBO();
        BeanUtils.copyProperties(this, customerBO);
        BeanUtils.copyProperties(vo, customerBO);
        customerBO.setContact(contactName);
        customerBO.setPhone(contactPhone);
        customerBO.setCustomerName(contactName);
        customerBO.setXzqhdm(areaCode.substring(0, areaCode.indexOf("_")));
        customerBO.setSaveType(CustomerSaveTypeEnum._my);
        customerBO.setAddCustomer(Boolean.TRUE);
        customerBO.setCustomerNature(CustomerNatureEnum.END_USRE_GOVERNMENT);
        if (StrUtil.isNotEmpty(houseType)) {
            customerBO.setBuildingType(Integer.valueOf(houseType.substring(0, houseType.indexOf("_"))));
        }
        if (StrUtil.isNotEmpty(heatingEnd)) {
            customerBO.setHeatingEnd(Integer.valueOf(heatingEnd.substring(0, heatingEnd.indexOf("_"))));
        }
        if (StrUtil.isNotEmpty(buildingStructure)) {
            customerBO.setBuildingStructure(buildingStructure.substring(0, buildingStructure.indexOf("_")));
        }
        if (StrUtil.isNotEmpty(housingArea)) {
            customerBO.setHeatingArea(new BigDecimal(housingArea));
            customerBO.setFloorSpace(new BigDecimal(housingArea));
            customerBO.setCommonHeatingArea(new BigDecimal(housingArea));
        }
        if (StrUtil.isNotEmpty(heatingMode)) {
            customerBO.setHeatingMode(Integer.valueOf(heatingMode.substring(0, heatingMode.indexOf("_"))));
        }
        if (StrUtil.isNotEmpty(energyType)) {
            customerBO.setEnergyType(EnergyTypeEnum.getEnum(energyType.substring(0, energyType.indexOf("_"))));
            if (EnergyTypeEnum._0.equals(customerBO.getEnergyType())) {
                customerBO.setElectricityMeterNumber(meterNumber);
            }
            if (EnergyTypeEnum._1.equals(customerBO.getEnergyType())) {
                customerBO.setGasMeterNumber(meterNumber);
            }
        }

        return customerBO;
    }

    private SaleOrderBO buildOrderBO(ImportExcelOfEndCustomerVO vo) {
        SaleOrderBO saleOrderBO = new SaleOrderBO();
        saleOrderBO.setAddOrder(Boolean.TRUE);
        saleOrderBO.setHandleForExcelImport(Boolean.TRUE);
        BeanUtils.copyProperties(vo, saleOrderBO);
        saleOrderBO.setSaleOrderName(contactName + " - 政府终端客户订单");
        saleOrderBO.setAreaCode(areaCode.substring(0, areaCode.indexOf("_")));
        saleOrderBO.setPhone(contactPhone);
        saleOrderBO.setInstallAddress(address);
        saleOrderBO.setEstimatedInstallTime(vo.getOperateDate());
        saleOrderBO.setRealTradingTime(vo.getOperateDate());
        saleOrderBO.setCompanyId(vo.getOperateUserCompanyId());
        saleOrderBO.setPayModel(SaleOrderPayModelEnum.PAIED_AFTER_INSTALL);
        saleOrderBO.setSaleOrderStatus("01".equals(vo.getSaveSoure()) ? SaleOrderStatusEnum.TO_BE_PAID : SaleOrderStatusEnum.PAID_SUCCESS);
        saleOrderBO.setOrderType(SaleOrderTypeEnum.PERSONAL);
        saleOrderBO.setCommit(Boolean.TRUE);
        saleOrderBO.setOtherCostPrice(BigDecimal.ZERO);
        saleOrderBO.setDiscount(100);

        List<SaleMasterBO> saleMasterBOList = new ArrayList<>();
        SaleMasterBO saleMasterBO = new SaleMasterBO();
        BeanUtils.copyProperties(saleOrderBO, saleMasterBO);
        saleMasterBO.setSaleMasterName("制热系统");
        saleMasterBO.setCompanyId(saleOrderBO.getCompanyId());
        List<SaleDetailBO> saleDetailBOList = new ArrayList<>();

        SaleDetailBO saleDetailBO = new SaleDetailBO();
        BeanUtils.copyProperties(saleOrderBO, saleDetailBO);
        if (StrUtil.isNotEmpty(deviceModel)) {
            String equipmentId = this.deviceModel.substring(0, this.deviceModel.indexOf("["));
            saleDetailBO.setEquipmentId(equipmentId);
        }
        saleDetailBO.setCount(1);
        saleDetailBO.setDiscount(100);
        saleDetailBO.setCompanyId(saleMasterBO.getCompanyId());
        saleDetailBOList.add(saleDetailBO);

        saleMasterBO.setDiscount(100);
        saleMasterBO.setSaleDetails(saleDetailBOList);
        saleMasterBOList.add(saleMasterBO);
        saleOrderBO.setSaleMasters(saleMasterBOList);

        return saleOrderBO;
    }

}
